import type {RouteMeta, RouteRecordRaw} from "vue-router";
import router from "../router/index";

type IRoute = RouteRecordRaw & {id?:string};

// 定义全部路由：Admin下的子一级。
const allRoutes:IRoute[] = [
    // {
    //     id:"1",
    //     name:"/",
    //     path: "",
    //     redirect: "/admin/homePage"
    // },
    {
        id:"1",
        name:"homePage",
        path: "homePage",
        component: () => import("../views/admin/homePage/homePages.vue")
    },
    {
        id:"2-1",
        name:"addGoods",
        path: "addGoods",
        component: () => import("../views/admin/shopss/addGoods.vue")
    },
    {
        id:"2-2",
        name:"shopLists",
        path: "shopLists",
        component: () => import("../views/admin/shopss/shopLists.vue")
    },
    {
        id:"3-1",
        name:"imgShow",
        path: "imgShow",
        component: () => import("../views/admin/banner/imgShow.vue")
    },
    {
        id:"3-2",
        name:"addImg",
        path: "addImg",
        component: () => import("../views/admin/banner/addImg.vue")
    },
    {
        id:"4-1",
        name:"manageList",
        path: "manageList",
        component: () => import("../views/admin/manages/manageList.vue")
    },
    {
        id:"4-2",
        name:"addManage",
        path: "addManage",
        component: () => import("../views/admin/manages/addManage.vue")
    },
   
    {
        id:"5-1",
        name:"showOrder",
        path: "showOrder",
        component: () => import("../views/admin/order/showOrder.vue")
    },
]      



export default function getRoutes(){
    console.log("getRoutes()");
    
    // 1、获取当前用户的权限
    let checkedKeys:any = [];
    const grants =  sessionStorage.getItem("grants");
    if(grants){
        checkedKeys = JSON.parse(grants);
    }
    if(checkedKeys.length===0){
        checkedKeys = ['1','2','2-1','2-2',,'3','3-1','3-1-U','3-1-D','3-2','4','4-1','4-1-U','4-1-D','4-2','5','5-1','5-1-U'];
    }

    // ["1","3-1","3-1-U","3-1-D","3"]

    // 2、获取当前用户的路由配置

    allRoutes.forEach(item=>{
        if(checkedKeys.includes(item.id as string)){ //
            // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
            let currRoute  = {...item}

            let arr = ["0","0","0","0"];
            if(checkedKeys.includes(item.id+"-C")){
                arr[0] = "1";
            }
            if(checkedKeys.includes(item.id+"-R")){
                arr[1] = "1";
            }
            if(checkedKeys.includes(item.id+"-U")){
                arr[2] = "1";
            }
            if(checkedKeys.includes(item.id+"-D")){
                arr[3] = "1";
            }

            currRoute.meta = {
                crud:arr.join("")
            }

            router.addRoute("admin",currRoute);

        }
    })


    console.log("router.getRoutes()",router.getRoutes());
}

export const  clearCurrentRoutes = ()=>{
   // 1、获取当前用户的权限
   let checkedKeys:string[] = [];
   const grants =  sessionStorage.getItem("grants");
   if(grants){
       checkedKeys = JSON.parse(grants);
   }
   if(checkedKeys.length===0){
       checkedKeys = ['1','2','2-1','2-2','2-3','3','3-1','3-2','4','4-1','4-2','5','5-1','6','6-1','7','7-1'];
   }

   // 2、删除当前用户的路由配置

   allRoutes.forEach(item=>{
       if(checkedKeys.includes(item.id as string)){
           // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
        //    router.addRoute("Admin",{...item});
          router.removeRoute(item.name as string);
       }
   })

}